define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ques_student/index' + location.search,
                    add_url: 'ques_student/add',
                    edit_url: 'ques_student/edit',
                    del_url: 'ques_student/del',
                    multi_url: 'ques_student/multi',
                    table: 'ques_student',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showExport: false,
                showToggle: false,
                showColumns: false,
                commonSearch: true,
                searchFormVisible: true,
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: '', title: __('序号'),operate:false,table:table,formatter: Table.api.formatter.natureorder},
                        // {field: 'openid', title: __('Openid')},
                        {field: 'nickname', title: __('Nickname'),operate:'like'},
                        {field: 'tel', title: __('Tel'),operate:false,operate:'like'},
                        {field: 'sid', title: __('Sid'),operate:false,operate:'like'},
                        // {field: 'classes_id', title: __('Classes_id')},
                        {field: 'classes_name', title: __('Classes_name'),operate:false},
                        // {field: 'college_id', title: __('College_id')},
                        {field: 'college_name', title: __('College_name'),operate:false},
                        {field: 'year_', title: __('Year_'),operate:false},
                        {field: 'province', title: __('Province'),operate:'like'},
                        {field: 'city', title: __('City'),operate:'like'},
                        {field: 'district', title: __('District'),operate:false},
                        {field: 'wx_formatted_address', title: __('Wx_formatted_address'),operate:false},
                        {field: 'a_1_text', title: __('是否有下列情况'),operate:false},
                        {field: 'a_1_1_text', title: __('发生时间'),operate:false},
                        {field: 'a_2_text', title: __('A_2'),operate:false},
                        {field: 'a_2_1_text', title: __('接触病例时间'),operate:false},
                        {field: 'a_3_text', title: __('所在社区（村居）21天内发生疫情'),operate:false},
                        {field: 'a_3_1_text', title: __('发生疫情时间'),operate:false},
                        // {field: 'a_3_2', title: __('A_3_2'),formatter: Table.api.formatter.yesorno,operate:false},
                        {field: 'a_4_text', title: __('近14天之内您是否有下列症状未痊愈'),operate:false},
                        {field: 'a_4_1', title: __('身体状况说明'),operate:false},
                        {field: 'a_11_text', title: __('14天内旅行史和接触史'),operate:false},
                        {field: 'a_11_1_text', title: __('时间'),operate:false},
                        {field: 'a_8_text', title: __('A_8'),operate:false},
                        {field: 'a_8_1_text', title: __('A_8_1'),operate:false},
                        {field: 'a_8_3_text', title: __('A_8_3'),operate:false},
                        {field: 'a_8_4_text', title: __('A_8_4'),operate:false},
                        {field: 'a_8_2_text', title: __('A_8_2'),operate:false},
                        {field: 'a_12_text', title: __('近14天有发热等症状未痊愈者'),operate:false},
                        {field: 'a_9_text', title: __('A_9'),operate:false},
                        {field: 'memo', title: __('备注'),operate:false},
                        {field: 'update_time', title: __('上报日期'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,defaultValue:Config.today},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $("#btn-report").click(function(){
                var that = this;
                Layer.confirm(__('请各学院部门检查核实数据准确后上报，您确定要上报吗?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    Backend.api.ajax({
                        url: "ques_student/report",
                        data: {type: 1}
                    }, function () {
                        $("#btn-report").attr("disabled",true);
                        $("#btn-report").removeClass("btn-danger");
                        $("#btn-report").addClass("btn-default");
                        $("#btn-report").html("已上报");
                        $(that).closest("tr").remove();
                        Layer.close(index);

                    });
                });
            })
            // 为表格绑定事件
            Table.api.bindevent(table);

            $("#exp").click(function () {
                var params = "";
                var create_time = $("input[name='update_time']").val();
                create_time = $.trim(create_time);
                if(create_time != ''){
                    params += '/create_time/' + create_time;
                }

                var province = $("input[name='province']").val();
                province = $.trim(province);
                if(province != ''){
                    params += '/province/' + province;
                }
                var city = $("input[name='city']").val();
                city = $.trim(city);
                if(city != ''){
                    params += '/city/' + city;
                }

                var sid = $("input[name='sid']").val();
                sid = $.trim(sid);
                if(sid != ''){
                    params += '/sid/' + sid;
                }

                var nickname = $("input[name='nickname']").val();
                nickname = $.trim(nickname);
                if(nickname != ''){
                    params += '/nickname/' + nickname;
                }

                if(Config.title !== '' && Config.answer !== ''){
                    params += '/title/'+Config.title+'/answer/'+Config.answer;
                }

                $("#exp").attr('href', 'exp'+params);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        abnormal: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ques_student/abnormal' + location.search,
                    add_url: 'ques_student/add',
                    edit_url: 'ques_student/edit',
                    del_url: 'ques_student/del',
                    multi_url: 'ques_student/multi',
                    table: 'ques_student',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showExport: false,
                showToggle: false,
                showColumns: false,
                commonSearch: true,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: '', title: __('序号'),operate:false,table:table,formatter: Table.api.formatter.natureorder},
                        // {field: 'openid', title: __('Openid')},
                        {field: 'nickname', title: __('Nickname'),operate:false},
                        {field: 'tel', title: __('Tel'),operate:false},
                        {field: 'sid', title: __('Sid'),operate:false},
                        // {field: 'classes_id', title: __('Classes_id')},
                        {field: 'classes_name', title: __('Classes_name'),operate:false},
                        // {field: 'college_id', title: __('College_id')},
                        {field: 'college_name', title: __('College_name'),operate:false},
                        {field: 'year_', title: __('Year_'),operate:false},
                        {field: 'province', title: __('Province'),operate:'like'},
                        {field: 'city', title: __('City'),operate:'like'},
                        {field: 'district', title: __('District'),operate:false},
                        {field: 'wx_formatted_address', title: __('Wx_formatted_address'),operate:false},
                        {field: 'a_1', title: __('A_1'),formatter: Table.api.formatter.qingkuang,operate:false},
                        {field: 'a_1_1', title: __('A_1_1'),operate:false, formatter: Table.api.formatter.datestr},
                        {field: 'a_1_2', title: __('今天的体温'),operate:false},
                        {field: 'a_2', title: __('A_2'),formatter: Table.api.formatter.jiechu,operate:false},
                        {field: 'a_2_1', title: __('接触病例时间'),operate:false, formatter: Table.api.formatter.datestr},
                        {field: 'a_3', title: __('A_3'),formatter: Table.api.formatter.yixue,operate:false},
                        {field: 'a_3_1', title: __('医学观察时间'),operate:false, formatter: Table.api.formatter.datestr},
                        {field: 'a_3_2', title: __('A_3_2'),formatter: Table.api.formatter.yesorno,operate:false},
                        {field: 'a_4', title: __('A_4'),formatter: Table.api.formatter.shengti,operate:false},
                        {field: 'a_4_1', title: __('身体状况说明'),operate:false},
                        {field: 'a_5', title: __('A_5'),formatter: Table.api.formatter.jiating,operate:false},
                        {field: 'a_5_1', title: __('家庭成员接触病例时间'),operate:false, formatter: Table.api.formatter.datestr},
                        {field: 'a_8_text', title: __('是否在外实习'),operate:false},
                        {field: 'a_8_1', title: __('实习地址'),operate:false},
                        {field: 'a_8_2', title: __('今日体温'),operate:false},
                        {field: 'memo', title: __('备注'),operate:false},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('填写详情'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('填写详情'),
                                    title: __('填写详情'),
                                    //classname: 'btn btn-xs btn-danger btn-dialog',
                                    classname: function(row) {
                                        if(row.info){
                                            return 'btn btn-xs btn-success btn-dialog';
                                        }else{
                                            return 'btn btn-xs btn-danger btn-dialog';
                                        }
                                    },
                                    icon: 'fa fa-list',
                                    url: 'ques_student/setinfo',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                            ],
                            operate:false,
                            formatter: Table.api.formatter.buttons
                        },

                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'province', title: __('Province')},
                        // {field: 'province_code', title: __('Province_code')},
                        // {field: 'city', title: __('City')},
                        // {field: 'city_code', title: __('City_code')},
                        // {field: 'district', title: __('District'),operate:false},
                        // {field: 'district_code', title: __('District_code')},
                        // {field: 'wx_province', title: __('Wx_province')},
                        // {field: 'wx_city', title: __('Wx_city')},
                        // {field: 'wx_district', title: __('Wx_district')},
                        // {field: 'wx_formatted_address', title: __('Wx_formatted_address'),operate:false},
                        {field: 'update_time', title: __('上报日期'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,defaultValue:Config.today},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $("#btn-report").click(function(){
                var that = this;
                Layer.confirm(__('请各学院部门检查核实数据准确后上报，您确定要上报吗?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    Backend.api.ajax({
                        url: "ques_student/report",
                        data: {type: 1}
                    }, function () {
                        $("#btn-report").attr("disabled",true);
                        $("#btn-report").removeClass("btn-danger");
                        $("#btn-report").addClass("btn-default");
                        $("#btn-report").html("已上报");
                        $(that).closest("tr").remove();
                        Layer.close(index);

                    });
                });
            })
            // 为表格绑定事件
            Table.api.bindevent(table);

            $("#abexp").click(function () {
                var params = "";
                var create_time = $("input[name='update_time']").val();
                create_time = $.trim(create_time);
                if(create_time != ''){
                    params += '/create_time/' + create_time;
                }

                var province = $("input[name='province']").val();
                province = $.trim(province);
                if(province != ''){
                    params += '/province/' + province;
                }
                var city = $("input[name='city']").val();
                city = $.trim(city);
                if(city != ''){
                    params += '/city/' + city;
                }
                $("#abexp").attr('href', 'abexp'+params);
            });
        },
        setinfo: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});